package com.anjubao.parking.municipal.roadside.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Component
public class HttpUtils {

    private static final Logger LOG = LoggerFactory.getLogger(HttpUtils.class);

    @Autowired
    private RestTemplate restTemplate;

    public <T> String post(String url, T data, Map<String, String> params) {
        LOG.debug("POST URL: " + url);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<T> entity = null;
        if (data == null) {
            entity = new HttpEntity<>(headers);
        } else {
            entity = new HttpEntity<>(data, headers);
        }
        HttpEntity<String> ret = null;
        if (params == null) {
             ret = restTemplate.postForEntity(url, entity, String.class);
        } else {
            ret = restTemplate.postForEntity(url, entity, String.class, params);
        }
        return ret.getBody();
    }


    /**
     *  post请求，失败后重发机制
     * @param url
     * @param data
     * @param repeatTime
     * @param secondTime
     * @param <T>
     * @return
     */
    public <T> String postRepeat(String url, T data,int repeatTime,int secondTime){
        String rep=this.postForEntity(url,data);
        int count=1;
        while (rep.equals("repeat")){
            try {
                Thread.sleep(secondTime*1000);
            }catch (Exception e){}
            if (count<=repeatTime){
                LOG.info("发送失败，url地址:{} ======>第{}次重发",url,count);
                rep= this.postForEntity(url,data);
            }else {
                break;
            }
            count++;
        }
        return rep;
    }

    public String postForm(String url, Map<String, String> form) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        for (Map.Entry<String, String> entry : form.entrySet()) {
            params.add(entry.getKey(), entry.getValue());
        }
        HttpEntity<MultiValueMap<String, String>> reqEntity = new HttpEntity<>(params, headers);
        try{
            HttpEntity<String> ret = restTemplate.postForEntity(url, reqEntity, String.class);
            return ret.getBody();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public <T> String postForEntity(String url, T data) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<T> entity = null;
        if (data == null) {
            entity = new HttpEntity<>(headers);
        } else {
            entity = new HttpEntity<>(data, headers);
        }
        String rep="";
        try{
            HttpEntity<String> ret = restTemplate.postForEntity(url, entity, String.class);
            rep=ret.getBody();
        }catch (Exception e){
            e.printStackTrace();
            rep="repeat";
        }
        return rep;
    }
}
